package com.niubi.yygh.hosp.testmongo;

import com.niubi.yygh.common.result.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/mongo2")
public class TestMongo2 {

    @Autowired
    private UserRepository userRepository;

    @GetMapping("findQuery")
    public R findQuery(){
        User user = new User();
        user.setName("张三");
        user.setAge(20);
        Example<User> example = Example.of(user);
        List list = userRepository.findAll(example);
        return R.ok().data("list",list);
    }

    @GetMapping("findLike")
    public R findLike(){
        //创建模糊查询匹配器
        ExampleMatcher matcher = ExampleMatcher.matching()
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写
        User user = new User();
        user.setName("三");
        Example example = Example.of(user,matcher);
        List list = userRepository.findAll(example);
        return R.ok().data("list",list);
    }

    @GetMapping("findData1")
    public R findData1(){
        String name = "张三";
        int age  = 20;
        List<User> list = userRepository.findByNameAndAge(name,age);
        return R.ok().data("list",list);
    }

    @GetMapping("findData2")
    public R findData2(){
        String name = "张三";
        List<User> list = userRepository.findByNameLike(name);
        return R.ok().data("list",list);
    }
}